﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Security.Principal;
using System.Diagnostics;
using System.ComponentModel;
using Win32.WtsApi32;
using System.Reflection;
using System.Threading;
using System.IO;

namespace RDPAddins
{
    public class Program
    {
        static System.Configuration.Configuration Configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        public static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var entry = AppDomain.CurrentDomain.GetData("entry");
            if (entry != null)
            {
                var Client = new RDPClient();
                Client.MainForm = new SlaveMode(Client);
                Client.MainForm.Load += new EventHandler((sender, e) =>
                {
                    ChannelReturnCodes ret = Client.Initialize(Assembly.GetExecutingAssembly().Location, (IntPtr)entry);
                    AppDomain.CurrentDomain.SetData("ret", ret == ChannelReturnCodes.Ok);
                    ((ManualResetEvent)AppDomain.CurrentDomain.GetData("event")).Set();
                    if (ret != ChannelReturnCodes.Ok)
                    {
                        MessageBox.Show("VirtualChannelInit error.\n" + ret.ToString(), "RDPAddins", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Application.Exit();
                    }
                });
                Application.Run(Client.MainForm);
            }
            else
            {
                //WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
                //if (!pricipal.IsInRole(WindowsBuiltInRole.Administrator))
                //{
                //    ProcessStartInfo processInfo = new ProcessStartInfo();
                //    processInfo.Verb = "runas";
                //    processInfo.Arguments = string.Join(" ", args);
                //    processInfo.FileName = Application.ExecutablePath;
                //    try
                //    {
                //        Process.Start(processInfo);
                //    }
                //    catch (Win32Exception)
                //    {
                //    }
                //}
                //else
                    Application.Run(new MasterMode());
            }
        }
        [STAThread]
        public static int MainC(string args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var entry = new IntPtr(long.Parse(new string(args.Reverse().ToArray())));
            ManualResetEvent wait = new ManualResetEvent(false);
            int mainret = 1;
            var thread = new Thread(() =>
            {
                var Client = new RDPClient();
                Client.MainForm = new SlaveMode(Client);
                Client.MainForm.Load += new EventHandler((sender, e) =>
                {

                    ChannelReturnCodes ret = Client.Initialize(Assembly.GetExecutingAssembly().Location, entry);
                    wait.Set();
                    if (ret != ChannelReturnCodes.Ok)
                    {
                        mainret = 0;
                        MessageBox.Show("VirtualChannelInit error.\n" + ret.ToString(), "RDPAddins", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Application.Exit();
                    }
                });
                Application.Run(Client.MainForm);
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            wait.WaitOne();
            return mainret;
        }   
    }
}
